<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Event;

class ProductEvaluate extends Base
{
    protected $name = 'product_evaluate';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')
            ->field('id,concat_ws("", left(account, 2), "**") as account,photo,concat_ws("-", nickname,code,mobile) as account2');
    }

    public function product()
    {
        return $this->hasOne(OrderProduct::class, 'id', 'order_product_id')
            ->field('id,title,icon,sku_name,vip_price,num');
    }

    public function orders()
    {
        return $this->hasOne(Order::class, 'id', 'order_id')
            ->field('id,describe_score,logistics_score,service_score');
    }


    public function getImagesAttr($value)
    {
        return $value ? explode(',', $value) : [];
    }

    public function getReplyTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '未回复';
    }

    public static function getProductEvaluate($pid, $limit = 20)
    {
        return parent::where(['pid' => $pid, 'is_del' => 0])
            ->with('user')
            ->field('id,uid,content,images,score,create_time')
            ->order('create_time', 'desc')
            ->limit($limit)
            ->select();
    }


    public static function getProductEvaluateCount($pid)
    {
        return parent::where(['pid' => $pid, 'status' => 1, 'is_del' => 0])->count();
    }


    public static function evaluateSave($param, $user): array
    {
        parent::startTrans();
        try {
            $info = Order::where(['uid' => $user->id, 'id' => $param['id'], 'is_del' => 0])
                ->field('id,sn,status,business_id')->find();
            if (empty($info)) return ['code' => -1, 'msg' => '订单错误'];
            if ($info->getData('status') != 4) {
                return ['code' => -1, 'msg' => '该订单无法评价'];
            }
            $data = [];
            foreach ($param['product'] as &$item) {
                $product = Product::where('id', $item['pid'])->field('id,title')->find();
                if (empty($product)) {
                    return ['code' => -1, 'msg' => '商品不存在或已下架'];
                }
                if ($item['score'] <= 0) {
                    return ['code' => -1, 'msg' => '《' . $product->title . '》商品评分错误'];
                }
                $item['uid'] = $user->id;
                $item['business_id'] = $param['business_id'];
                $item['order_id'] = $info->id;
                $item['order_product_id'] = $item['id'];
                $item['create_time'] = time();
                $item['update_time'] = time();
                $data[] = $item;
            }
            (new ProductEvaluate())->insertAll($data);
            $info->status = 5;
            $info->comment_time = time();
            $info->describe_score = $param['describe_score'];
            $info->logistics_score = $param['logistics_score'];
            $info->service_score = $param['service_score'];
            $info->save();
            //店铺评分事件
            Event::trigger('BusinessScore', ['business_id' => $param['business_id']]);
            parent::commit();
            return ['code' => 1, 'msg' => '评价成功'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
